package com.todo.ln.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.todo.ln.entity.Goods;
import com.todo.ln.mapper.GoodsMapper;
import com.todo.ln.service.GoodsService;
import com.todo.ln.utils.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 1.@description:
 * 2.@author:ln
 * 3.@Date:2022/9/14
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    //查所有
    public RestUtils goodsList() {
        List<Goods> goods = goodsMapper.selectList(null);
        return new RestUtils(200,"",goodsMapper.selectList(null));
    }

    @Override
    // 根据商品名称模糊查询
    public RestUtils goodsListByLikeGoodName(String goodsName) {
        return new RestUtils(200,"查询到的商品信息",goodsMapper.selectList(new QueryWrapper<Goods>().like("goods_name",goodsName)));
    }
    // 分页查询逻辑
    public RestUtils goodsListByPage(Integer current,Integer rows){
        // 1.第一步分页查询数据
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<Goods>(current,rows),null);
        // 2.存储返回的数据----HashMap
        HashMap<String,Object> map = new HashMap<>();
        // 3.存储当前页的所有数据
        map.put("goods",goodsPage.getRecords());
        // 4.存储总页数
        map.put("pages",goodsPage.getPages());
        // 5.存储总条数
        map.put("total",goodsPage.getTotal());
        // 返回RestUtils结果集
        return new RestUtils(200,"分页查询成功！",map);
    }

    @Override
    // 增加商品
    public RestUtils addGoods(Goods goods){
        return goodsMapper.insert(goods) > 0 ? new RestUtils(200,"添加商品信息成功！",null) : new RestUtils(500,"添加商品信息失败！",null);
    }
    // 修改商品
    public RestUtils updateGoods(Goods goods){
        return goodsMapper.updateById(goods) >0? new RestUtils(200,"修改成功！",null):new RestUtils(500,"修改失败！",null);
    }
    // 逻辑删除商品
    public  RestUtils delGoods(Integer goodsId){
        return goodsMapper.delete(new QueryWrapper<Goods>().eq("goods_id",goodsId)) > 0 ?
                new RestUtils(200,"删除成功！",null):new RestUtils(500,"删除失败！",null);
    }


}
